package com.example.volunteer.service.impl;

import com.example.volunteer.exception.CustomException;
import com.example.volunteer.mapper.SkillMapper;
import com.example.volunteer.model.Skill;
import com.example.volunteer.service.SkillService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SkillServiceImpl implements SkillService {

    @Autowired
    private SkillMapper skillMapper;

    @Override
    @Transactional
    public void insert(Skill skill) {
        if (skill.getSkillContent() == null || skill.getSkillContent().trim().isEmpty()) {
            throw new CustomException("技能内容不能为空");
        }
        // 检查技能是否已存在
        Skill existingSkill = skillMapper.findByContent(skill.getSkillContent());
        if (existingSkill != null) {
            throw new CustomException("该技能已存在");
        }
        skillMapper.insert(skill);
    }

    @Override
    @Transactional
    public void addVolunteerSkill(Integer skillId, String volunteerId) {
        if (skillId == null || volunteerId == null || volunteerId.trim().isEmpty()) {
            throw new CustomException("参数不能为空");
        }
        // 检查关联是否已存在
        Integer count = skillMapper.countVolunteerSkill(skillId, volunteerId);
        if (count > 0) {
            throw new CustomException("该志愿者已拥有此技能");
        }
        skillMapper.addVolunteerSkill(skillId, volunteerId);
    }

    @Override
    @Transactional
    public void removeAllVolunteerSkills(String volunteerId) {
        if (volunteerId == null || volunteerId.trim().isEmpty()) {
            throw new CustomException("志愿者ID不能为空");
        }
        skillMapper.removeAllVolunteerSkills(volunteerId);
    }

    @Override
    public PageResult<Skill> findByVolunteerId(String volunteerId, int pageNum, int pageSize) {
        if (volunteerId == null || volunteerId.trim().isEmpty()) {
            throw new CustomException("志愿者ID不能为空");
        }

        int offset = (pageNum - 1) * pageSize;
        List<Skill> skills = skillMapper.findByVolunteerIdWithPage(volunteerId, offset, pageSize);
        int total = skillMapper.countByVolunteerId(volunteerId);

        return createPageResult(skills, total, pageNum, pageSize);
    }

    @Override
    public PageResult<Skill> findAll(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Skill> skills = skillMapper.findAllWithPage(offset, pageSize);
        int total = skillMapper.countAll();

        return createPageResult(skills, total, pageNum, pageSize);
    }

    private PageResult<Skill> createPageResult(List<Skill> data, int total, int pageNum, int pageSize) {
        PageResult<Skill> result = new PageResult<>();
        result.setData(data);
        result.setTotal(total);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setPages((int) Math.ceil((double) total / pageSize));
        return result;
    }
}
